package user

import (
	"encoding/json"
	"fmt"
	userService "forum/app/service/user"
	userData "forum/app/structData/user"
	"forum/app/tools"
	"net/http"
	"strconv"
)

func Login(w http.ResponseWriter, r *http.Request) {
	var requestData userData.LoginRequest
	json.NewDecoder(r.Body).Decode(&requestData)

	tools.TryCatch(func() {
		userInfo := userService.LoginService(requestData)
		tools.OutPutSuccess(w, userInfo)
	}, func(err any) {
		tools.OutPutError(w, fmt.Sprint(err))
	})
}

func GetUserInfo(w http.ResponseWriter, r *http.Request) {
	var requestData userData.GetUserInfoRequest
	json.NewDecoder(r.Body).Decode(&requestData)

	tools.TryCatch(func() {
		userInfo := userService.GetRedisUserInfo(strconv.Itoa(requestData.Id))
		tools.OutPutSuccess(w, userInfo)
	}, func(err any) {
		tools.OutPutError(w, fmt.Sprint(err))
	})
}
